from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains


# 打开Edge浏览器
driver = webdriver.Edge()
# 页面最大化
driver.maximize_window()
# 进入百度
driver.get('https://www.baidu.com/')
sleep(3)
# input1 = driver.find_element(By.TAG_NAME, 'input')
input1 = driver.find_element(By.ID, 'kw')
# 输入北京博物馆
input1.send_keys('北京博物馆')
sleep(2)
# 回车事件
input1.send_keys(Keys.ENTER)
sleep(2)
input1.clear()
sleep(2)
# 输入框输入长城
input1.send_keys('长城')
sleep(2)
# 点击百度一下
seatch1 = driver.find_element(By.ID, 'su')
seatch1.click()
sleep(2)
# 悬停在设置上
xt1 = driver.find_element(By.NAME, 'tj_settingicon')
ActionChains(driver).move_to_element(xt1).perform()
sleep(2)
# 点击高级搜索
click1 = driver.find_element(By.XPATH, '/html/body/div[3]/div[1]/div[2]/div/a[2]')
click1.click()
sleep(2)
# 输入包含全部关键词
input2 = driver.find_element(By.ID, 'adv_keyword')
input2.send_keys('北京')
sleep(1)
# 输入不包括关键词
input3 = driver.find_element(By.NAME, 'q4')
input3.send_keys('4')
sleep(1)
# 点击高级搜索按钮
button1 = driver.find_element(By.XPATH, '/html/body/div[3]/div[5]/div/div/div/div[2]/div/form/ul/li[6]/div/input[2]')
button1.click()
sleep(2)
# 窗口切换
driver.switch_to.window(driver.window_handles[-1])
sleep(3)
# 模糊查询点击
driver.find_element(By.PARTIAL_LINK_TEXT, '人民政府门户网站').click()
sleep(2)
driver.switch_to.window(driver.window_handles[-1])
sleep(2)
# 向下滚动任意像素
driver.execute_script('window.scrollTo(0, 1000)')
sleep(3)
# 向下滚动-滚动到底部
driver.execute_script('window.scrollTo(0, document.body.scrollHeight)')
sleep(3)
# 向上滚动-滚动到顶部
driver.execute_script('window.scrollTo(0, 0)')


sleep(10)

# # 关闭浏览器
# driver.quit()

















